#!/bin/sh
FILE=$1
i=`pdfinfo $FILE | grep Pages | awk '{print $2}'`
echo $i

	echo "selezionato \"$FILE\""

                mkdir -p out
    		while (("$i">0)) 
    		do 
    			gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -sColorConversionStrategy=RGB -dProcessColorModel=/DeviceRGB -dPDFSETTINGS=/prepress -dQUIET -dNOPAUSE -dBATCH -dSAFER -dFirstPage=$(($i-2)) -dLastPage=$i -sOutputFile=out/scp_$(($i/3)).pdf $FILE 1>&2
			echo $(($i/3));
			fp="out/scp_$(($i/3)).pdf"

			fn1=`pdftotext $fp - | sed -n '83p'`
			fn2=`pdftotext $fp - | sed -n '81p'`
    			echo $(($i/3))-DAT-$fn1 - $fn2
			mv "$fp" "out/$(($i/3))-DAT-$fn1 - $fn2.pdf"
    			i=$(($i-3)) 
    		done

#    		for fp in $(ls out/scp*.pdf)
#    		do
#			fn1=`pdftotext $fp - | sed -n '83p'`
#			fn2=`pdftotext $fp - | sed -n '98p'`
#			echo $fn1 $fn2
#    		        mv "$fp" "out/$fn1-$fn2.pdf"
#    		
#    		done
